from bisect import bisect_left

class Robot(object):
    # self, max energy
    def __init__(self, me):
        # x, y, max energy, energy
        self.x = 0
        self.y = 0
        self.me = me
        self.e = me

    def closest(self, dust):
        sel = (self.x, self.y)
        pos = bisect_left(dust, sel)
        if pos == 0:
            return dust[0], pos
        if pos == len(dust):
            return dust[-1], pos
        bef = dust[pos - 1]
        aft = dust[pos]
        sm = self.x + self.y
        if (aft[0] + aft[1]) - sm < sm - (bef[0] + bef[1]):
            return aft, pos
        else:
            return bef, (pos - 1 if len(dust) is not 0 else pos)

    def set_pos(self, xy):
        self.x = xy[0]
        self.y = xy[1]

    def dist(self, xy):
        return abs(self.x - xy[0]) + abs(self.y - xy[1])

    def energy(self):
        return self.e

    def recharge(self):
        print('recharged!')
        self.e = self.me

    def drive(self, xy, way):
        print('robot is at', self.x, self.y, 'driving to', xy[0], xy[1], 'with a dist of', way)
        if (xy[0] == 0) and (xy[1] == 0):
            self.x = 0
            self.y = 0
            self.recharge()

        self.x, self.y = xy[0], xy[1]
        self.e -= way
